public class reverseBetweenNodes {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        int len = right - left + 1;
        ListNode dump = new ListNode(-1);
        dump.next = head;
        int leftTimes = left - 1;
        ListNode preNode = dump;
        while (leftTimes-- > 0) {
            preNode = preNode.next;
        }
        ListNode rightNode = preNode;
        while (len-- > 0) {
            rightNode = rightNode.next;
        }
        ListNode leftNode = preNode.next;
        preNode.next = null;
        ListNode nextNode = rightNode.next;
        rightNode.next = null;
        helper(leftNode);
        preNode.next = rightNode;
        leftNode.next = nextNode;
        return dump.next;


    }


    public ListNode helper(ListNode node) {
        if (node == null || node.next == null) return node;

        ListNode pre = node;
        ListNode cur = node.next;
        ListNode next = helper(node.next);
        cur.next = pre;
        pre.next = null;
        return next;
    }
}
